#include "collision.h"

// 点碰撞箱
PointCollisionBox::PointCollisionBox()
    : CollisionBox(CollisionBoxType::Point), x_(0), y_(0) {}
PointCollisionBox::PointCollisionBox(float x, float y)
    : CollisionBox(CollisionBoxType::Point), x_(std::abs(x)), y_(std::abs(y)) {}

float PointCollisionBox::getX() const { return x_; }
float PointCollisionBox::getY() const { return y_; }

bool PointCollisionBox::setX(float x) {
    if (x >= 0) {
        x_ = x;
        return true;
    }
    return false;
}
bool PointCollisionBox::setY(float y) {
    if (y >= 0) {
        y_ = y;
        return true;
    }
    return false;
}

// 多态碰撞
bool PointCollisionBox::checkCollision(const CollisionBox& other) const {
    return other.checkCollisionWithPoint(*this);
}

// 点与点碰撞: 完全重合
bool PointCollisionBox::checkCollisionWithPoint(const PointCollisionBox& point) const {
    return (x_ == point.x_) && (y_ == point.y_);
}

// 点与矩形的碰撞检测: 点在矩形内
bool PointCollisionBox::checkCollisionWithRectangle(const RectangleCollisionBox& rect) const {
    return (x_ >= rect.getX() && x_ <= rect.getX() + rect.getWidth() &&
        y_ >= rect.getY() && y_ <= rect.getY() + rect.getLength());
}

// 点与圆形的碰撞检测: 点在圆内
bool PointCollisionBox::checkCollisionWithCircle(const CircleCollisionBox& circle) const {
    float dx = x_ - circle.getX();
    float dy = y_ - circle.getY();
    return (dx * dx + dy * dy) <= (circle.getRadius() * circle.getRadius());
}